package com.sunjs.model.back;

import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

@JsonInclude(Include.NON_NULL)
public class RestResponse<T> implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private int code;
	private String message;
	private T result;

	public static <T> RestResponse<T> success() {
		return new RestResponse<T>();
	}

	public static <T> RestResponse<T> success(T result) {
		RestResponse<T> response = new RestResponse<T>();
		response.setResult(result);
		return response;
	}

	public static <T> RestResponse<T> error(RestCode code) {
		return new RestResponse<T>(code.code, code.message);
	}

	public RestResponse() {
		this(RestCode.SUCCESS.code, RestCode.SUCCESS.message);
	}

	public RestResponse(int code, String message) {
		this.code = code;
		this.message = message;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public T getResult() {
		return result;
	}

	public void setResult(T result) {
		this.result = result;
	}

	@Override
	public String toString() {
		return "RestResponse [code=" + code + ", message=" + message + ", result=" + result + "]";
	}
	
	public static <T> RestResponse<T> checkError(int code,String message) {
		return new RestResponse<T>(code, message);
	}

}
